//
//  LTFJokeView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/27.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFJokeView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFSeventhTableViewCell.h"
#import "LTFEightTableViewCell.h"
@interface LTFJokeView ()
<
UITableViewDataSource,
UITableViewDelegate
>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewArray;
@end

@implementation LTFJokeView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.frame style:UITableViewStylePlain];
    [self addSubview:_tableView];
    _tableView.tag = 1011;
    [_tableView release];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[LTFSeventhTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFSeventhTableViewCell class])];
    [_tableView registerClass:[LTFEightTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFEightTableViewCell class])];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFJokeModel *model = self.tableViewArray[indexPath.row];
    if (model.imgsrc == nil) {
        LTFSeventhTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFSeventhTableViewCell class])];
        
        cell.model = model;
        return cell;
    }
    else {
        LTFEightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFEightTableViewCell class])];
        
        cell.model = model;
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFJokeModel *model = self.tableViewArray[indexPath.row];
    [self.delegate sendAddress:model.docid];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LTFJokeModel *model = self.tableViewArray[indexPath.row];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    CGRect rect =[model.digest boundingRectWithSize:CGSizeMake(self.frame.size.width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    if (model.pixel != nil) {
        NSArray *array = [model.pixel componentsSeparatedByString:@"*"];
        CGFloat h = (self.frame.size.width - 20) * [array[1] floatValue] / [array[0] floatValue];        return rect.size.height + 100 + h;
    }
    else {
        return rect.size.height + 100;
    }
    
}

- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string retain];
        [self createData];
    }
}
- (void)createData {
    
    self.tableViewArray = [NSMutableArray array];
    
    NSString *string = @"http://c.3g.163.com/recommend/getChanListNews?channel=T1419316284722&size=20&passport=rGc7w7sHvkZrAZuUjPI1uYGhEZ0p7R6sR1ulitv8uwnL2AgtwrES0PPiPHJrLH2UePBK0dNsyevylzp8V9OOiA%3D%3D&devId=uSlbMJ2oW%2BxBMe0P%2BksNTw%3D%3D&lat=&lon=&version=6.1&net=wifi&ts=1461635913&sign=m1CQY33gpSTGIJwwu9IxftvDOX1E3Ue77MgKVHGp9hJ48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=netease_3g";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = dic[@"段子"];
        self.tableViewArray = [LTFJokeModel BaseModelByArray:array];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
